package cc.rengu.igas.channel.nucc.biz.impl;

import cc.rengu.igas.channel.nucc.facade.request.*;
import cc.rengu.igas.channel.nucc.facade.response.*;
import cc.rengu.igas.channel.nucc.common.constant.NuccServiceConstant;
import cc.rengu.igas.channel.nucc.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.nucc.facade.NuccAcqPayService;
import cc.rengu.igas.channel.nucc.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

/**
 * 网联受理侧接口
 *
 * @author caoyanbin
 * @date 20200415
 */
public class NuccAcqPayServiceImpl implements NuccAcqPayService {
    RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    /**
     * 快捷协议-身份认证及签约
     *
     * @param request 快捷协议-身份认证及签约请求
     * @return 快捷协议-身份认证及签约响应
     */
    @Override
    public Result<VerifySignResponse> verifySignQuick(VerifySignRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<VerifySignResponse> result = new Result<>();
        VerifySignResponse verifySignResponse = new VerifySignResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(request,
                    NuccServiceConstant.VERIFY_SIGN_QUICK_SERVICE, VerifySignResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((VerifySignResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("网联快捷协议失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                verifySignResponse.setRespCode(((BizException) e).getCode());
                verifySignResponse.setRespDesc(e.getMessage());
            } else {
                verifySignResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                verifySignResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    /**
     * 解约
     *
     * @param request 解约请求
     * @return 解约响应
     */
    @Override
    public Result<RescindSignResponse> rescindSign(RescindSignRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<RescindSignResponse> result = new Result<>();
        RescindSignResponse rescindSignResponse = new RescindSignResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(request,
                    NuccServiceConstant.RESCIND_SIGN_SERVICE, TransSeqNoApplyResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((RescindSignResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("网联解约失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                rescindSignResponse.setRespCode(((BizException) e).getCode());
                rescindSignResponse.setRespDesc(e.getMessage());
            } else {
                rescindSignResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                rescindSignResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    /**
     * 协议查询
     *
     * @param request 协议查询请求
     * @return 协议查询响应
     */
    @Override
    public Result<SignStateQueryResponse> signStateQuery(SignStateQueryRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<SignStateQueryResponse> result = new Result<>();
        SignStateQueryResponse signStateQueryResponse = new SignStateQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(request,
                    NuccServiceConstant.SIGN_QUERY_SERVICE, TransSeqNoApplyResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((SignStateQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("网联协议查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                signStateQueryResponse.setRespCode(((BizException) e).getCode());
                signStateQueryResponse.setRespDesc(e.getMessage());
            } else {
                signStateQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                signStateQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    /**
     * 协议支付
     *
     * @param request 协议支付请求
     * @return 协议支付响应
     */
    @Override
    public Result<CommonTransResponse> agreementPay(AgreementPayRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CommonTransResponse> result = new Result<>();
        CommonTransResponse commonTransResponse = new CommonTransResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(request,
                    NuccServiceConstant.AGREEMENT_PAY_SERVICE, TransSeqNoApplyResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((CommonTransResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("网联协议支付失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                commonTransResponse.setRespCode(((BizException) e).getCode());
                commonTransResponse.setRespDesc(e.getMessage());
            } else {
                commonTransResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                commonTransResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    /**
     * 商户付款
     *
     * @param request 商户付款请求
     * @return 商户付款响应
     */
    @Override
    public Result<CommonTransResponse> mchntPayment(MchntPaymentRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CommonTransResponse> result = new Result<>();
        CommonTransResponse commonTransResponse = new CommonTransResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(request,
                    NuccServiceConstant.MCHNT_PAYMENT_SERVICE, TransSeqNoApplyResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((CommonTransResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("网联付款申请失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                commonTransResponse.setRespCode(((BizException) e).getCode());
                commonTransResponse.setRespDesc(e.getMessage());
            } else {
                commonTransResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                commonTransResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    /**
     * 退款
     *
     * @param request 退款请求
     * @return 退款响应
     */
    @Override
    public Result<CommonTransResponse> refundPayment(RefundPaymentRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CommonTransResponse> result = new Result<>();
        CommonTransResponse commonTransResponse = new CommonTransResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(request,
                    NuccServiceConstant.REFUND_PAYMENT_SERVICE, TransSeqNoApplyResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((CommonTransResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("网联退款失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                commonTransResponse.setRespCode(((BizException) e).getCode());
                commonTransResponse.setRespDesc(e.getMessage());
            } else {
                commonTransResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                commonTransResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    /**
     * 交易状态查询
     *
     * @param request 交易状态查询请求
     * @return 交易状态查询响应
     */
    @Override
    public Result<TransStateQueryResponse> transStateQuery(TransStateQueryRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<TransStateQueryResponse> result = new Result<>();
        TransStateQueryResponse transStateQueryResponse = new TransStateQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(request,
                    NuccServiceConstant.TRANS_STATE_QUERY_SERVICE, TransSeqNoApplyResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((TransStateQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("网联交易状态查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                transStateQueryResponse.setRespCode(((BizException) e).getCode());
                transStateQueryResponse.setRespDesc(e.getMessage());
            } else {
                transStateQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                transStateQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    /**
     * 交易详情查询
     *
     * @param request 交易详情查询请求
     * @return 交易详情查询响应
     */
    @Override
    public Result<TransDetailQueryResponse> transDetailQuery(TransDetailQueryRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<TransDetailQueryResponse> result = new Result<>();
        TransDetailQueryResponse transDetailQueryResponse = new TransDetailQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(request,
                    NuccServiceConstant.TRANS_DETAIL_QUERY_SERVICE, TransSeqNoApplyResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((TransDetailQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("网联交易详情查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                transDetailQueryResponse.setRespCode(((BizException) e).getCode());
                transDetailQueryResponse.setRespDesc(e.getMessage());
            } else {
                transDetailQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                transDetailQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }


    @Override
    public Result<BusinessAuthSignResponse> businessAuthSign(BusinessAuthSignRequest businessAuthSignRequest) throws Exception {
        DubboService dubboService = new DubboServiceImpl();
        Result<BusinessAuthSignResponse> responseResult = new Result<>();
        try {
            BusinessAuthSignResponse businessAuthSignResponse = new BusinessAuthSignResponse();
            DubboResult dubboResult = dubboService.callAtomService(businessAuthSignRequest,
                    NuccServiceConstant.BUSINESS_AUTH_SIGN_SERVICE, BusinessAuthSignResponse.class);
            responseResult.setSuccess(dubboResult.isSuccess());
            responseResult.setResult((BusinessAuthSignResponse) dubboResult.getResult());
        } catch (Exception e) {

        }
        return responseResult;
    }
}
